package cn.com.gensoft.app.core.exception;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常管理  demo  待完善
 * @author hlj
 */
@RestControllerAdvice
public class BusinessExceptionHandler {

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 自定义异常
	 */
	@ExceptionHandler(BusinessException.class)
	public ResponseEntity<BusinessException> handleRRException(BusinessException e){

		return ResponseEntity.badRequest().body(new BusinessException("业务异常",e));
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public ResponseEntity<String> handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);
		return ResponseEntity.badRequest().body("数据库中已存在该记录");
	}

	@ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
	public ResponseEntity<String> handleAuthorizationException(AuthorizationException e){
		logger.error(e.getMessage(), e);
        return ResponseEntity.badRequest().body("权限不足！");
	}

	@ExceptionHandler(Exception.class)
	public ResponseEntity<String>  handleException(Exception e){
		logger.error(e.getMessage(), e);
		return ResponseEntity.badRequest().body("服务器错误！！！");
	}

}
